package com.winshare.saleAssistant.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;

public class Encodes {
    private static final String DEFAULT_URL_ENCODING = "UTF-8";
    private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
    private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final char[] DIGITS_UPPER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public Encodes() {
    }

    public static String encodeHex(byte[] input, boolean toLowerCase) {
        return new String(toLowerCase ? encodeHexText(input, DIGITS_LOWER) : encodeHexText(input, DIGITS_UPPER));
    }

    public static byte[] decodeHex(String input) throws DecoderException {
        return decodeHex(input.toCharArray());
    }

    public static String encodeBase64(byte[] input) {
        return new String(Base64.getEncoder().encode(input));
    }

    public static String encodeBase64(String input) throws EncoderException {
        try {
            return new String(Base64.getEncoder().encode(input.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException var2) {
            throw new EncoderException("base64编码失败:", var2);
        }
    }

    public static byte[] decodeBase64(String input) {
        return Base64.getDecoder().decode(input.getBytes());
    }

    public static String decodeBase64String(String input) throws DecoderException {
        try {
            return new String(Base64.getDecoder().decode(input.getBytes()), "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            throw new DecoderException("Base64解码失败.", var2);
        }
    }

    public static String encodeBase62(byte[] input) {
        char[] chars = new char[input.length];

        for(int i = 0; i < input.length; ++i) {
            chars[i] = BASE62[(input[i] & 255) % BASE62.length];
        }

        return new String(chars);
    }

    public static String urlEncode(String part) throws EncoderException {
        try {
            return URLEncoder.encode(part, "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            throw new EncoderException("Url编码失败", var2);
        }
    }

    public static String urlDecode(String part) throws DecoderException {
        try {
            return URLDecoder.decode(part, "UTF-8");
        } catch (UnsupportedEncodingException var2) {
            throw new DecoderException("URL解码失败", var2);
        }
    }

    public static byte[] decodeHex(char[] data) throws DecoderException {
        int len = data.length;
        if ((len & 1) != 0) {
            throw new DecoderException("Hex解码失败:Odd number of characters.");
        } else {
            byte[] out = new byte[len >> 1];
            int i = 0;

            for(int j = 0; j < len; ++i) {
                int f = toDigit(data[j], j) << 4;
                ++j;
                f |= toDigit(data[j], j);
                ++j;
                out[i] = (byte)(f & 255);
            }

            return out;
        }
    }

    protected static int toDigit(char ch, int index) throws DecoderException {
        int digit = Character.digit(ch, 16);
        if (digit == -1) {
            throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index);
        } else {
            return digit;
        }
    }

    protected static char[] encodeHexText(byte[] data, char[] toDigits) {
        int l = data.length;
        char[] out = new char[l << 1];
        int i = 0;

        for(int var5 = 0; i < l; ++i) {
            out[var5++] = toDigits[(240 & data[i]) >>> 4];
            out[var5++] = toDigits[15 & data[i]];
        }

        return out;
    }
}
